package com.ruoyi.user.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.reservationserver.entity.User;
import com.ruoyi.reservationserver.pojo.RechargePojo;
import com.ruoyi.reservationserver.utils.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.user.domain.Golfer;
import com.ruoyi.user.service.IGolferService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 球友管理Controller
 * 
 * @author ruoyi
 * @date 2023-08-12
 */
@RestController
@RequestMapping("/golfer/user")
public class GolferController extends BaseController
{
    @Autowired
    private IGolferService golferService;

    /**
     * 会员充值
     * @param
     * @return
     */
    @PostMapping("/recharge")
    public Result recharge(@RequestBody RechargePojo rechargePojo){
        Golfer golfer = golferService.selectGolferByUserId(rechargePojo.getUserId());
        golfer.setIsMember(rechargePojo.getIsMember());
        if (golferService.updateGolfer(golfer)==1){
            return Result.success();
        }else {
            return Result.error("更新失败");
        }


    }


    /**
     * 查询球友管理列表
     */
    @PreAuthorize("@ss.hasPermi('golfer:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(Golfer golfer)
    {
        startPage();
        List<Golfer> list = golferService.selectGolferList(golfer);
        return getDataTable(list);
    }

    /**
     * 导出球友管理列表
     */
    @PreAuthorize("@ss.hasPermi('golfer:user:export')")
    @Log(title = "球友管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Golfer golfer)
    {
        List<Golfer> list = golferService.selectGolferList(golfer);
        ExcelUtil<Golfer> util = new ExcelUtil<Golfer>(Golfer.class);
        util.exportExcel(response, list, "球友管理数据");
    }

    /**
     * 获取球友管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('golfer:user:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(golferService.selectGolferByUserId(userId));
    }

    /**
     * 新增球友管理
     */
    @PreAuthorize("@ss.hasPermi('golfer:user:add')")
    @Log(title = "球友管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Golfer golfer)
    {
        return toAjax(golferService.insertGolfer(golfer));
    }

    /**
     * 修改球友管理
     */
    @PreAuthorize("@ss.hasPermi('golfer:user:edit')")
    @Log(title = "球友管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Golfer golfer)
    {
        return toAjax(golferService.updateGolfer(golfer));
    }

    /**
     * 删除球友管理
     */
    @PreAuthorize("@ss.hasPermi('golfer:user:remove')")
    @Log(title = "球友管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(golferService.deleteGolferByUserIds(userIds));
    }
}
